package com.ruoyi.factory.qylock.controller.api;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.factory.common.utils.RequestUtil;
import com.ruoyi.factory.qylock.domain.EsignFileVO;
import com.ruoyi.factory.qylock.service.QysHandleService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLDecoder;
import java.util.Map;


/**
 * @ClassName QylockHandleController
 * @Description: TODO 契约锁业务处理类
 * @Author LiuJiaji
 * @Date 2023/6/5 0005 16:54
 **/
@RestController
@RequestMapping("/api/qysHandle")
public class QysHandleController {

    @Autowired
    private QysHandleService qysHandleService;

    /**
     * @author: Liujiaji
     * @date: 2023/6/6 0006 17:37
     * @description: TODO 契约锁回调处理
     * @param: [prejson]
     * @return: com.ruoyi.common.core.domain.AjaxResult
     **/
    @PostMapping("/upStatusNotify")
    public AjaxResult upStatus(HttpServletRequest request){
        Map<String, String> notifyResult = RequestUtil.getRequestParams(request);
        return qysHandleService.upStatus(notifyResult);
    }

    /**
     * @author: Liujiaji
     * @date: 2023/6/8 0008 14:13
     * @description: TODO 契约锁电子签章文件下载
     * @param: [req, res]
     * @return: void
     **/
    @RequestMapping("/getEsignFileDown")
    public void getEsignFileDown(HttpServletRequest req, HttpServletResponse res) throws FileNotFoundException, UnsupportedEncodingException {
        String pathstr = req.getParameter("pathstr");
        pathstr = URLDecoder.decode(pathstr, "utf-8");
        qysHandleService.getEsignFileDown(res, pathstr);
    }


    /**
     * @author: Liujiaji
     * @date: 2023/6/15 0015 13:56
     * @description: TODO 固定模板数据填充
     * @param: [request]
     * @return: com.ruoyi.common.core.domain.AjaxResult
     **/
    @PostMapping("/eSignByTemplate")
    public AjaxResult eSignByTemplate(@RequestBody String request) {
        return qysHandleService.eSignByTemplate(request);
    }

    /**
     * @author: Liujiaji
     * @date: 2023/6/26 0026 15:29
     * @description: TODO 获取签章流程
     * @param: [request]
     * @return: com.ruoyi.common.core.domain.AjaxResult
     **/
    @PostMapping("/normalTemplateInfo")
    public AjaxResult normalTemplateInfo(@RequestBody String request){
        return qysHandleService.normalTemplateInfo(request);
    }

    /**
     * @author: Liujiaji
     * @date: 2023/6/27 0027 10:54
     * @description: TODO 根据文件发起签章
     * @param: [request]
     * @return: com.ruoyi.common.core.domain.AjaxResult
     **/
    @PostMapping("/signByFileqys")
    public AjaxResult signByFileqys (@RequestBody String request){
        return qysHandleService.signByFileqys(request);
    }

    /**
     * @author: Liujiaji
     * @date: 2023/6/27 0027 10:54
     * @description: TODO 签署页面跳转
     * @param: [request]
     * @return: com.ruoyi.common.core.domain.AjaxResult
     **/
    @PostMapping("/querySignPageUrl")
    public AjaxResult querySignPageUrl (@RequestBody String request){
        return qysHandleService.querySignPageUrl(request);
    }



    public static void main(String[] args) {
        String sql = "select distinct salh.vbillcode htbm, salh.subscribedate qdrq, salh.vdef10 qsdd, salh.ntotalastnum zsl, salh.ntotalorigmny zje,\n" +
                "salh.invallidate zzrq, salh.vdef5 fkfs,\n" +
                "salb.crowno hh, salb.nastnum sl, salb.nqtorigtaxprice hsdj, salb.norigtaxmny jshj, salb.ntaxmny je, salb.vbdef10 khcpmc, salb.vbdef9 khcpgg,\n" +
                "cush.name dwmc, cush.legalbody fr, cush.tel1 dhh,cush.fax1 cz,  cush.taxpayerid sh,\n" +
                "addh.detailinfo dwdz, banh.accnum zh, banh.combineaccname khyh,\n" +
                "math.name cpmc, math.materialspec cpgg, defc.name bzw, meah.name dw,\n" +
                "incb.paymentday zqts,incch.name xsxymc, defcc.name yslx\n" +
                "from \n" +
                "ct_sale salh \n" +
                "left join CT_SALE_B salb on salb.pk_ct_sale = salh.pk_ct_sale and salb.dr=0\n" +
                "left join bd_customer cush on cush.PK_CUSTOMER = salh.pk_customer\n" +
                "left join BD_MATERIAL_V math on math.pk_source = salb.pk_material\n" +
                "left join bd_defdoc defc on defc.pk_defdoc = salb.vfree1\n" +
                "left join bd_measdoc meah on meah.pk_measdoc = salb.castunitid\n" +
                "left join bd_defdoc defcc on defcc.pk_defdoc = salh.vdef3\n" +
                "left join BD_INCOME inch on inch.pk_income = salh.pk_payterm and inch.dr=0\n" +
                "left join BD_INCOMECH incb on incb.pk_payment = inch.pk_income and incb.dr=0 and incb.prepayment ='N'\n" +
                "left join bd_incomeperiod incch on incch.pk_incomeperiod = incb.pk_incomeperiod and incch.dr=0\n" +
                "left join bd_custbank custb on custb.pk_cust = cush.pk_customer and custb.dr=0 and custb.isdefault = 'Y'\n" +
                "left join bd_bankaccbas banh on banh.pk_bankaccbas = custb.pk_bankaccbas and banh.dr=0\n" +
                "left join bd_address addh on addh.pk_address = cush.corpaddress  and addh.dr=0\n" +
                "where salh.pk_ct_sale = ? and salh.dr=0  and salh.BLATEST='Y' ORDER BY salb.crowno asc";

        System.out.println(sql);

    }


}


